<template>
	<view class="data-box">
		<view class="data-fixed-box">
			<search @searchText="searchText" :placeholder="placeholder" :showSearch="true"></search>
			<!-- 选择产品或选择客户 -->
			<productORcustomer></productORcustomer>
			<filters v-if="isShowFilters" 
					@setST="setST" 
					@setHandlers="setHandlers"  
					@setStoreId="setStoreId"
					:filltersData="filltersData" 
					@setTime="setTime">
			</filters>
		</view>
		<DocumentsList @setBillsInfoData="setBillsInfoData" 
					:DocumentsLs="DocumentsLs" 
					@orderList="orderList"
					ref="setBillsId"
					:setHetght="isShowFilters" >
		</DocumentsList>
	</view>
</template>

<script>
	import search from "../comm/Search.vue";
	import productORcustomer from "./productORcustomer.vue";
	import filters from "./Documentsfilters.vue";
	import DocumentsList from "./DocumentsList.vue";
	import staticConst from "../../js/const/StaticConst.js";
	import config from "../../js/conf/Config.js";
	let pageNo = 0;
	let pageSize = staticConst.PAGE_SIZE;
	export default {
		components: { search , productORcustomer , filters , DocumentsList },
		data() {
			return {
				isShowFilters : true,//是否显示过滤栏
				showBillsState : true,//filters组件的状态过滤显示
				orderData : {
					// ch : 1,//渠道可不传
					st : 1,//单据状态
					kw : '',//搜索关键词
					storeid : '',//仓库ID
					uid_cp : '',//经手人ID
					time : '',//时间类型
				},
				DocumentsLs : [],
				filltersData : {
					cat : "类别",
					store : "全部仓库",
					inStock : "全部产品",
					screening : "筛选",
					statisticalIndex : 2,
					TimeStateIndex : '',
					StoresStateIndex : '',
					HandlersStateIndex : '',
				},
				placeholder:"搜索货号/品名/货名/备注"
			};
		},
		
		methods: {
			
			initData()
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderList(1);
			},
			
			/*** 搜索 ***/
			searchText(res)
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderData.kw = res;
				setTimeout( () => {
					this.orderList('ss');
				},200)
			},
			
			/*** 检测是否展示过滤栏 ***/
			setFilters(res,index)
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderData = {
					st : 1,//单据状态
					kw : '',//搜索关键词
					storeid : '',//仓库ID
					uid_cp : '',//经手人ID
					time : '',//时间类型
				};
				this.isShowFilters = res;
				if(index == staticConst.BILLS_CLOUD)
				{
					this.orderList(1);
					return;
				}
				console.log(index)
				if(index == staticConst.BILLS_RETURN || 
					index == staticConst.BILLS_DRAFT || 
					index == staticConst.BILLS_INVALID)
				{
					this.showBillsState = false;
				}else
				{
					this.showBillsState = true;
				}
				this.orderData.st = index;
				this.orderList();
				this.filltersData = {
					cat : "类别",
					store : "全部仓库",
					inStock : "全部产品",
					screening : "筛选",
					statisticalIndex : 2,
					TimeStateIndex : '',
					StoresStateIndex : '',
					HandlersStateIndex : '',
				};
			},
			
			setBillsInfoData(res)
			{
				let orderData={};
				orderData.type = res.type;
				orderData.odid = res.id;
				this.$emit("setBillsThree",orderData);
				this.$refs.setBillsId.setBillsId(res.id);
			},
			
			/*** 设置订单筛选 ***/
			setST(res)
			{
				pageNo = 0;
				this.billsLs = [];
				if(res == staticConst.BILLS_CLOUD)
				{
					this.orderList(1);
					return;
				}
				console.log(res)
				if( res == staticConst.BILLS_RETURN || 
					res == staticConst.BILLS_DRAFT || 
					res == staticConst.BILLS_INVALID )
				{
					this.showBillsState = false;
				}else
				{
					this.showBillsState = true;
				}
				this.orderData.st = res;
				this.orderList();
			},
			
			/*** 设置时间删选 ***/
			setTime(res)
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderData.time = res;
				this.orderList();
			},
			
			/*** 设置经手人筛选 ***/
			setHandlers(res)
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderData.uid_cp = res;
				this.orderList();
			},
			
			/*** 设置门店筛选 ***/
			setStoreId(res)
			{
				pageNo = 0;
				this.billsLs = [];
				this.orderData.storeid = res;
				this.orderList();
			},
			
			/*** 云单/销售单据列表  ORDER_LS ***/
			async orderList(index)
			{
				let url = config.ORDER_LS;
				let param = {};
				if(index == 1)
				{
					param.ch = 2;
					param.pfrom = pageNo * pageSize;
					param.pnum = pageSize;
				}else
				{
					param = this.orderData;
					param.pfrom = pageNo * pageSize;
					param.pnum = pageSize;
				}
				
				let res = await this.$api({url:url,data:param});
				
				if(!res.data) return;
				if( index == 'ss')
				{
					this.billsLs = res.data;
				}else
				{	
					this.billsLs = this.billsLs.concat(res.data);
				}
				if(this.billsLs.length > 0 && pageNo == 0)
				{
					let orderData={};
					orderData.type = this.billsLs[0].type;
					orderData.odid = this.billsLs[0].id;
					this.$refs.setBillsId.setBillsId(this.billsLs[0].id);
					this.$emit("setBillsThree",orderData);
				}
				pageNo ++;
			},
		},
	}
</script>
<style lang="scss">
.data-box
{
	position: fixed;
	margin-left: 72px;
	box-sizing: border-box;
	width: 344px;
	height: 100vh;
	border-right: 1px solid #ECECEC;
	.data-fixed-box
	{	
		width: 345px;
		box-sizing: border-box;
		border-right: 1px solid #ECECEC;
		position: fixed;
		top: 0;
		background: #FFF;
		z-index: 1;
	}
}
</style>